<?php
// TODO Error messages
class OverallTentCapacityValidatorSchema extends sfValidatorSchema
{
  const OPTION_OVERALL_CAPACITY_HOUSE = 'overall_capacity_house';
  const OPTION_OVERALL_CAPACITY_TENT = 'overall_capacity_tent';

  protected function configure ($options = array(), $messages = array())
  {
    $this->addOption(self::OPTION_OVERALL_CAPACITY_HOUSE, 0);
    $this->addOption(self::OPTION_OVERALL_CAPACITY_TENT, 0);
  }

  protected function doClean ($values)
  {
    $capacity_house = 0;
    $capacity_tent = 0;
    foreach ($values as $key => $value)
    {
      if (is_array($value) && isset($value['capacity']))
      {
        if ($value['type'] == 'h')
        {
          $capacity_house += $value['capacity'];
        }
        else
        {
          $capacity_tent += $value['capacity'];
        }
      }
    }
    
    $errorSchema = new sfValidatorErrorSchema($this);
    if ($capacity_house != $this->getOption(self::OPTION_OVERALL_CAPACITY_HOUSE))
    {
      $errorSchema->addError(new sfValidatorError($this, 'Du hast nicht so viele Leute für Haus angegeben, wie zuvor angegeben.'), 'capacity_mismatch_house');
    }
    if ($capacity_tent != $this->getOption(self::OPTION_OVERALL_CAPACITY_TENT))
    {
      $errorSchema->addError(new sfValidatorError($this, 'Du hast nicht so viele Leute auf Zelte verteilt, wie zuvor angegeben'), 'capacity_mismatch_tent');
    }
    if (count($errorSchema) > 0)
    {
      throw $errorSchema;
    }
    
    return $values;
  }
}